package com.example.demo.service.impl;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.DTO.LoginUserInDto;
import com.example.demo.entity.websocket.User;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.BusinessExceptionStatus;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserNewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserNewServiceImpl implements UserNewService {
    @Resource
    private UserMapper userMapper;
    @Override
    public User login(LoginUserInDto inDto){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getUserName,inDto.getUsername())
                .eq(User::getStatus,1);
        User user = userMapper.selectOne(userQueryWrapper);
        if(Validator.isEmpty(user)) throw new BusinessException(BusinessExceptionStatus.USER_NOT_FOUND);
        if(!user.getPassword().equals(inDto.getPassword())) throw  new BusinessException(BusinessExceptionStatus.PASSWORD_NOT_CORRECT);
        return user;
    }
}
